﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ScannerClasses;

namespace Program
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void openfiledialogButton_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = ""; // Default file name
           // dlg.DefaultExt = ".txt"; // Default file extension
          //  dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension 

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results 
            if (result == true)
            {
                // Open document 
                filesourceTextbox.Text = dlg.FileName;
            }
        }

        private void saveLogButton_Click(object sender, RoutedEventArgs e)
        {
            // Configure save file dialog box
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = "Log"; // Default file name
            dlg.DefaultExt = ".text"; // Default file extension
            dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension 

            // Show save file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process save file dialog box results 
            if (result == true)
            {
                // Save document 
                logsavepathTextbox.Text = dlg.FileName;
            }
        }

        private async void runScanButton_Click(object sender, RoutedEventArgs e)
        {
            filesourceTextbox.IsEnabled = false;
            openfiledialogButton.IsEnabled = false;
            logsavepathTextbox.IsEnabled = false;
            saveLogButton.IsEnabled = false;
            runScanButton.IsEnabled = false;
            statusLabel.Content = "Running";
            try
            {
                fileTextBlock.Text += File.ReadAllText(filesourceTextbox.Text);
                IScanner scan = new Scanner(filesourceTextbox.Text);
                await Task.Delay(200);
                while (scan.HasNext())
                {
                    logTextBlock.Text += scan.Scan().ToString() + "\r\n";
                }
            }catch(Exception ex){
                logTextBlock.Text += "Problem with the file\r\n";
                logTextBlock.Text += ex.ToString();
            }
            statusLabel.Content = "Finished";
            filesourceTextbox.IsEnabled = true;
            openfiledialogButton.IsEnabled = true;
            logsavepathTextbox.IsEnabled = true;
            saveLogButton.IsEnabled = true;
            runScanButton.IsEnabled = true;
        }

    }
}
